const app = getApp();

export const notesMixin = {
  data: {
    pageSize: 20,
    currentPage: 1,
    total: 0,
    isFinish: false, //是否加载完全部数据
    len: 0,
  },
  onPullDownRefresh() {
    console.log("下拉刷新");
    this.getDeclarationList();
  },
  onReachBottom() {
    let page = this.data.currentPage;
    let isFinish = this.data.isFinish;
    if (!isFinish) {
      this.getDeclarationList(page + 1);
    }
    // console.log("上啦加载")
  },
  async getDeclarationList(page = 1) {
    const response = await this.$_server("declaration/list", {
      params: {
        id: "?currentPage=" + page + "&pageSize=" + "15",
      },
    });
    let { data } = response;

    if (data.code != 200) {
      return;
    }

    //处理数据
    let records = data.data.records;
    if (records) {
      if (records.length == 1 && Object.keys(records[0]).length == 0) {
        records = [];
      }
    } else {
      records = [];
    }
    let mapRecords = records[0];
    //  let recordsCount = 0;

    if (page == 1) {
    } else {
      let new_records = this.data.records ?? [];
      //加到最后
      records = new_records.concat(records);
    }

    if (mapRecords) {
    } else if (mapRecords) {
      mapRecords = [];
    }
    this.setData({
      records: records,
      currentPage: page,
      isFinish: mapRecords ? false : true, //全部加载完毕
      len: mapRecords ? Object.values(mapRecords).flat(Infinity).length : 0,
    });
  },
  checkShowAlert() {
    if (app.user.token.length > 0) {
      //判断一下
      let openId = app.user.userInfo.openId;
      let showAlert = app.user.userInfo.showAlert;
      var that = this;
      if (openId.length == 0 && showAlert == true) {
        // 绑定
        app.user.userInfo.showAlert = false;
        wx.showModal({
          title: "微信绑定提醒",
          content: "你还没有绑定微信，绑定微信后可接收微信消息提醒。",
          cancelText: "绑定微信", //默认是“取消”
          confirmText: "下次绑定",
          success: function (res) {
            if (res.confirm) {
              // console.log('用户点击确定')
            } else if (res.cancel) {
              that.bindWxAction();
            }
          },
        });
      }
    }
  },
};
